package reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reggie.common.R;
import reggie.entity.Category;
import reggie.entity.Dish;
import reggie.service.CategoryService;
import reggie.service.DishService;
import reggie.service.EmployeeService;
import sun.awt.geom.AreaOp;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;
    /**
     * 新增菜品或套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody  Category category) {
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> list(int page, int pageSize) {
        //创建分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //排序条件
        queryWrapper.orderByAsc(Category::getType).orderByAsc(Category::getUpdateTime);
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据ID删除分类
     *
     * @param
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids) {
        categoryService.remove(ids);
        return R.success("删除分类成功");
    }

    /**
     * 根据ID修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updata(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    //根据条件查询对应菜品数据
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (category.getType() != null) {
            queryWrapper.eq(Category::getType, category.getType());
            List<Category> list = categoryService.list(queryWrapper);
            return R.success(list);
        }else {
            queryWrapper.in(Category::getType, 1, 2);
            queryWrapper.orderByAsc(Category::getType).orderByDesc(Category::getUpdateTime);
            List<Category> list = categoryService.list(queryWrapper);
            return R.success(list);
        }
    }

}
